<%--
  Created by IntelliJ IDEA.
  User: 沸羊羊
  Date: 2022/11/19
  Time: 22:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>物料组成变更</title>
</head>
<body>
<div class="row" style="margin-top: 50px;margin-right: 20px;">
    <div class="col-md-offset-7 col-md-5" >
        <input type="button" onclick="addUpdateModule()" class="btn btn-primary" value="添加物料">
        <input type="button" onclick="delUpdateModule()" class="btn btn-default" value="删除物料">
        <input type="button" class="btn btn-default" value="重新确认" onclick="UpdateModuleConfirm()">
        <input type="button" class="btn btn-default" value="返回" onclick="UpdateModulereturn()">
    </div>
</div>
<form id="UpdateModuleForm">
    <div class="container" style="width: 800px;height: 550px;border: black solid 1px;">
        <h2 align="center">物料组成设计单</h2>
        <br>
        <div class="row">
            <div class="col-md-offset-2 col-md-4">
                <b>设计单编号</b>
                <input type="text" name="designid" id="UpdateModuleDesignId" readonly style="border: none;outline: none">
            </div>
            <div class="col-md-offset-1 col-md-5">
                <b>设计人：</b>
                <input type="text" style="outline: none;border-width: 0px 0px 1px 0px;width: 150px" id="UpdateModule" value="${username}" readonly>
            </div>
        </div>
        <br>
        <div class="row">
            <div class="col-md-offset-2 col-md-4">
                <b>产品名称：</b>
                <input id="UpdateModuleProductName" style="outline: none;border:none;width: 150px" readonly name="productname">
            </div>
            <div class="col-md-offset-1 col-md-5">
                <b>产品编号：</b>
                <input id="UpdateModuleProductId" style="outline: none;border:none;width: 150px"  readonly name="productid">
            </div>
        </div>
        <br>
        <br>
        <table id="UpdateModuleTab">

        </table>
        <br>
        <div class="row">
            <div class="col-md-offset-2 col-md-4">
                <b>登记人：</b>
                <input type="text" style="outline: none;border-width: 0px 0px 1px 0px;background-color: #E5F4EF;width: 150px" id="UpdateModuleRegister" value="${username}" readonly>
            </div>
            <div class="col-md-offset-1 col-md-5">
                <b>登记时间：</b>
                <span id="UpdateModuleRegisterTime"></span>
            </div>
        </div>
        <br>
        <div class="row">
            <div class="col-md-offset-2 col-md-10">
                <b style="position: relative;top: -41px;">设计要求：</b>
                <textarea id="UpdateModuleModuleDescribe" style="width: 450px;height: 95px;resize: none;" name="moduledescribe"></textarea>
            </div>
        </div>
    </div>
</form>
</div>

<div class="modal fade  texts" data-backdrop="static" id="UpdateModuleModal" style="width:900px;margin-left: 500px">
    <div class="modal-dialog" style="width: 788px">
        <div class="modal-content" id="UpdateModuleModalContent">

        </div>
    </div>
</div>

<script>
    //明细表格数据
    $("#UpdateModuleTab").bootstrapTable({
        height:200,
        uniqueId: "productid",                     //每一行的唯一标识，一般为主键列
        columns:[
            {
                field:"",
                title: "点选",
                checkbox:true
            },
            {
                field: "productid",
                title: "物料编号"
            },
            {
                field: "productname",
                title: "物料名称"
            },
            {
                field: "type",
                title: "用途类型",
                formatter:function (value,row,index) {
                    if(value=="Y001-2"){
                        return "物料";
                    }else{
                        return "商品";
                    }
                }
            },
            {
                field: "productdescribe",
                title: "描述"
            },
            {
                field: "amount",
                title:"数量",
                formatter:function (value,row,index) {
                    return "<input type='number' style='outline: none;border: none' value='"+value+"' onchange='MaterialCompositionDesignSheetChangeAmount(this)'>";
                }
            },
            {
                field: "amountunit",
                title: "单位"
            },
            {
                field: "costprice",
                title: "计划成本单价(元)"
            },
            {
                field: "subtotal",
                title: "小计(元)"
            }
        ]
    })
    //改变数量
    function MaterialCompositionDesignSheetChangeAmount(obj) {

        //获取表单里所有的数据
        var all=$("#UpdateModuleTab").bootstrapTable("getData")

        //获取当前改变数量的那个产品的物料编号
        var productId=$(obj).parent().parent().find("td:eq(1)").text();
        $(all).each(function (index,item) {
            if(item.productid==productId){
                console.log(productId)
                item.amount=$(obj).val();
                item.subtotal=$(obj).parent().next().next().text()*$(obj).val()
                $("#UpdateModuleTab").bootstrapTable("load",all)
            }
        })


    }

    //确定
    function UpdateModuleConfirm() {

        //获取表单里的数据
        var module=$("#UpdateModuleForm").serializeArray();
        var module2 = {};
        $(module).each(function (index,item) {
            module2[item.name]=item.value;
        })
        //获取表格的所有数据
        var obj=$("#UpdateModuleTab").bootstrapTable("getData");

        module2["moduledetails"]=obj;

        console.log(module2)

        $.ajax({
            url:"UpdateModule.action",
            data:JSON.stringify(module2),
            dataType:"json",
            contentType:"application/json",
            type:"post",
            success:function (data) {
                alert(data.msg)
                //点击返回到制定物料组成设计单
                    $("#tab1").addTabs("物料组成设计单变更","","Materialcompositiondesignsheetchanged.jsp","",true);
                    $("#tab1").closeTabs("物料组成变更")
            }
        })

    }

    //点击返回到制定物料组成设计单
    function UpdateModulereturn(){
        $("#tab1").addTabs("物料组成设计单变更","","Materialcompositiondesignsheetchanged.jsp","",true);
        $("#tab1").closeTabs("物料组成变更")
    }

    //删除物料重新添加
    function delUpdateModule() {

        //获取你选中的物料
        var cks=$("#UpdateModuleTab").bootstrapTable("getSelections");

        if(cks.length==0){
            alert("请选择一个在删除");
            return ;
        }else{
            $(cks).each(function (index,item) {
                $("#UpdateModuleTab").bootstrapTable("removeByUniqueId",item.productid)
            })
        }

    }

    function MaterialCompositionDesignSheetShowTime() {
        //获取当前时间
        var time=new Date();
        var current=time.getFullYear()+"/"+(time.getMonth()+1)+"/"+time.getDate()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
        $("#UpdateModuleRegisterTime").text(current)
    }

    //计算器
    $(function () {
        var id='${param.id}'
        $.ajax({
            url:"QueryModuleById.action/"+id,
            type:"post",
            dataType:"json",
            success:function (data1) {
                $("#UpdateModuleDesignId").val(data1.designid);
                $("#UpdateModuleProductName").val(data1.productname);
                $("#UpdateModuleProductId").val(data1.productid);
                $("#UpdateModuleModuleDescribe").val(data1.moduledescribe);
                console.log(data1)
                $(data1.moduledetails).each(function (index,data) {
                    var arr=[{
                        "productid":data.productid,
                        "productname":data.productname,
                        "type":data.type,
                        "productdescribe":data.productdescribe,
                        "amount":data.amount,
                        "amountunit":data.amountunit,
                        "costprice":data.costprice,
                        "subtotal":data.subtotal,
                    }];
                    $("#UpdateModuleTab").bootstrapTable("append",arr);
                })
            }
        })
        setInterval(MaterialCompositionDesignSheetShowTime(),1000)
    })


    //添加物料的方法
    function addUpdateModule() {
        $("#UpdateModuleModalContent").load("AddUpdateModule.jsp")
        $("#UpdateModuleModal").modal("show")
    }


</script>
</body>
</html>
